package com.zhao.controller;

import com.zhao.mapper.TreeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TreeController {
    @Autowired
    private TreeMapper treeMapper;


    /**
     * 查询下级
     *
     * @param id
     * @param level
     * @return
     */
    @GetMapping("/getTree")
    public List<Map<String, Object>> findTree(@RequestParam("id") String id, @RequestParam("level") Integer level) {
        Integer id1 = Integer.valueOf(id);
        List<Map<String, Object>> map = new ArrayList<>();
        //只查询跟节点
        if (level == 0) {
            map = treeMapper.getTree(id1);
        }
        //查询下级
        else {
            map = treeMapper.getTreeByParentId(id1);
        }
        //判断是否有子节点
        for (Map<String, Object> item : map) {
            List<Map<String, Object>> childrenData = treeMapper.getTreeByParentId(id1);
            if (childrenData.size() > 0) {
                item.put("isLeaf", false);
            } else {
                item.put("isLeaf", true);
            }
        }
        return map;
    }

    @RequestMapping("/getCity")
    public List<Map<String, Object>> findCity(@RequestParam("id")String id){
        Integer integer = Integer.valueOf(id);
        List<Map<String, Object>> map = new ArrayList<>();
        map=treeMapper.getCity(integer);
        return map;
    }
}
